package http

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mall-portalv2/conf"
	"mall-portalv2/global"
	"mall-portalv2/internal/server/ctx"
	"net/http"
)

/**
* Author: joker
* TODO: test
* Date: 2022/8/19
* Time: 上午9:22
**/

// @Summary 健康检查
// @Description 健康检查
// @Tags Ping
// @Accept  json
// @Produce  json
// @Success 200 {object} ginContext.Response
// @Failure 400 {object} ginContext.HTTPError
// @Failure 404 {object} ginContext.HTTPNotFound
// @Failure 500 {object} ginContext.HTTPServerError
// @Router /healthcheck
func healthCheck(c *gin.Context) {
	app := ctx.Gin{C: c}
	app.Response(http.StatusNoContent, ctx.StatusNoContent, "test ok")
}

// @Summary Ping
// @Description Ping
// @Tags Ping
// @Accept  json
// @Produce  json
// @Success 200 {object} ginContext.Response
// @Failure 400 {object} ginContext.HTTPError
// @Failure 404 {object} ginContext.HTTPNotFound
// @Failure 500 {object} ginContext.HTTPServerError
// @Router /healthcheck

func ping(ctx *gin.Context) {
	if conf.Config.Mysql.IsUse || conf.Config.Redis.IsUse {
		if err := svc.Ping(ctx); err != nil {
			global.Log.Error("ping ctx", zap.Error(err))
			ctx.AbortWithStatus(http.StatusServiceUnavailable)
		}
	}
}
